<template>
  <div class="container" >
	<!--工具栏-->
	<div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
		<el-form :inline="true" :model="filters" :size="size">
			<el-form-item>
				<el-input v-model="filters.label" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.search')" perms="sys:busTerm:view" type="primary" @click="findPage(null)"/>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.add')" perms="sys:busTerm:add" type="primary" @click="handleAdd" />
			</el-form-item>
		</el-form>
	</div>
	<!--表格内容栏-->
	<kt-table permsEdit="sys:busTerm:edit" permsDelete="sys:busTerm:delete" permsStart="sys:busTerm:start"  permsEnd= "sys:busTerm:end"
		:data="pageResult" :columns="columns" :showItemB="showItemB"
		@findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete" @handleEnd="handleEnd" @handleStart = "handleStart">
	</kt-table>
	<!--新增编辑界面-->
	<el-dialog :title="operation?'新增':'编辑'" width="20%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
		<el-form :model="dataForm"  :rules="dataFormRules" ref="dataForm" :size="size" >

			<el-form-item label="学年" prop="year"   >
				 <el-select v-model="dataForm.year" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in optionYear"
                    :key="item"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
			</el-form-item>
			<el-form-item label="学期" prop="term"   >
				<el-select v-model="dataForm.term" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in [1,2]"
                    :key="item"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
			</el-form-item>
      <el-form-item label="名称" prop="name"   >
				<el-input v-model="dataForm.name" style = "width:215px"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
			<el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
		</div>
	</el-dialog>
  </div>
</template>

<script>
import KtTable from "@/views/Core/KtTable"
import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
export default {
	components:{
			KtTable,
			KtButton
	},
	data() {
		return {
			size: 'small',
			filters: {
				label: ''
			},
			columns: [
				{prop:"name", label:"名称", minWidth:100},
				{prop:"year", label:"学年", minWidth:100},
				{prop:"term", label:"学期", minWidth:100},
				{prop:"status", label:"状态", minWidth:100,formatter: this.statusFormat},
			],
			pageRequest: { pageNum: 1, pageSize: 8 },
			pageResult: {},

			operation: false, // true:新增, false:编辑
			editDialogVisible: false, // 新增编辑界面是否显示
			editLoading: false,
			dataFormRules: {
				label: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				]
			},
			// 新增编辑界面数据
			dataForm: {
				id: null,
				name: null,
				year: null,
				term: null,
				status: null,
				createBy: null,
				createTime: null,
				lastUpdateBy: null,
				lastUpdateTime: null,
				delFlag: null,
			},
      optionYear:[2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025],
      showItemB:'showItemB'
		}
	},
	methods: {
		// 获取分页数据
		findPage: function (data) {
			if(data !== null) {
				this.pageRequest = data.pageRequest
			}
      this.pageRequest.params = [{name:'label', value:this.filters.label}]
			this.$api.busTerm.findPage(this.pageRequest).then((res) => {
				this.pageResult = res.data
			}).then(data!=null?data.callback:'')
		},
		// 批量删除
		handleDelete: function (data) {
			this.$api.busTerm.batchDelete(data.params).then(data!=null?data.callback:'')
		},
    // 开启填报
		handleStart: function (data) {
			this.$api.busTerm.startTerm(data.params).then(data!=null?data.callback:'')
		},
        // 结束填报
		handleEnd: function (data) {
			this.$api.busTerm.endTerm(data.params).then(data!=null?data.callback:'')
		},
		// 显示新增界面
		handleAdd: function () {
			this.editDialogVisible = true
			this.operation = true
			this.dataForm = {
				id: null,
				name: null,
				year: null,
				term: null,
				status: null,
				createBy: null,
				createTime: null,
				lastUpdateBy: null,
				lastUpdateTime: null,
				delFlag: null,
			}
		},
		// 显示编辑界面
		handleEdit: function (params) {
			this.editDialogVisible = true
			this.operation = false
			this.dataForm = Object.assign({}, params.row)
		},
		// 编辑
		submitForm: function () {
			this.$refs.dataForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true
						let params = Object.assign({}, this.dataForm)
						this.$api.busTerm.save(params).then((res) => {
							if(res.code == 200) {
								this.$message({ message: '操作成功', type: 'success' })
							} else {
								this.$message({message: '操作失败, ' + res.msg, type: 'error'})
							}
							this.editLoading = false
							this.$refs['dataForm'].resetFields()
							this.editDialogVisible = false
							this.findPage(null)
						})
					})
				}
			})
		},
  // 时间格式化
      	dateFormat: function (row, column, cellValue, index){
          	return format(row[column.property])
      	},
      statusFormat: function (row, column, cellValue, index){
        if(row[column.property] == 2){
            return '进行中';
        }else if(row[column.property] == 3){
            return '已完成';
        }else{
            return '未开始';
        }
            
     }


	},
	mounted() {
	}
}
</script>

<style scoped>

</style>
